package com.gdones.gpt.bus.webAccount.controller;


import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.xuyanwu.spring.file.storage.FileInfo;
import com.gdones.common.core.annotations.MySysLog;
import com.gdones.common.core.annotations.MySysLogOpration;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyResult;
import com.gdones.gpt.bus.webAccount.dto.GptWebaccount;
import com.gdones.gpt.bus.webAccount.dto.GptWebaccountInfo;
import com.gdones.gpt.bus.webAccount.mapper.GptWebaccountInfoMapper;
import com.gdones.gpt.bus.webAccount.service.IGptWebaccountInfoService;
import com.gdones.gpt.bus.webAccount.service.IGptWebaccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  租赁平台账号管理
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-05-20
 */
@RestController
@RequestMapping("/webAccount")
@Slf4j
@Api(tags = "账号库")
public class GptWebaccountController {
    
    @Resource(name = "gptWebaccountServiceImpl")
    private IGptWebaccountService gptWebaccountServiceImpl;

    @Resource(name = "gptWebaccountInfoServiceImpl")
    private IGptWebaccountInfoService gptWebaccountInfoServiceImpl;

    @Autowired
    private GptWebaccountInfoMapper infoMapper;

    @GetMapping("/pages")
    @ApiOperation("账号库-分页")
    @MySysLog(modelName="账号库",methodDesc = "分页",methodType = MySysLogOpration.GET)
    public MyResult pages(GptWebaccount dto, MyPageBand pageBand){

        log.debug("--账号库：分页");
        MyResult result = new MyResult();

        try {
            result = gptWebaccountServiceImpl.pages(dto,pageBand);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("账号库-分页-异常");
        }
        return result;
    }

    @GetMapping("/list")
    @ApiOperation("账号库-列表（不分页）")
    @MySysLog(modelName="账号库",methodDesc = "列表（不分页）",methodType = MySysLogOpration.GET)
    public MyResult list(GptWebaccount dto){

        log.debug("--账号库：列表（不分页）");
        MyResult result = new MyResult();

        try {
            result = gptWebaccountServiceImpl.mylist(dto);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("账号库-列表（不分页）-异常");
        }
        return result;
    }

    @GetMapping("/getByID")
    @ApiOperation("账号库-查询入库单明细")
    @MySysLog(modelName="账号库",methodDesc = "查询入库单明细",methodType = MySysLogOpration.GET)
    public MyResult getByID(String id){

        log.debug("--账号库：查询入库单明细");
        MyResult result = new MyResult();

        try {
            result = gptWebaccountServiceImpl.getByID(id);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("账号库-查询入库单明细-异常");
        }
        return result;
    }

    @GetMapping("/getAccountInfo")
    @ApiOperation("账号库-查询账号记录")
    @MySysLog(modelName="账号库",methodDesc = "查询账号记录",methodType = MySysLogOpration.GET)
    public MyResult getAccountInfo(String id){

        log.debug("--账号库：查询账号记录");
        MyResult result = new MyResult();

        try {
            result = gptWebaccountServiceImpl.getAccountInfo(id);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("账号库-查询账号记录-异常");
        }
        return result;
    }

    @PostMapping("/add")
    @ApiOperation("账号库-添加")
    @MySysLog(modelName="账号库",methodDesc = "添加",methodType = MySysLogOpration.POST)
    public MyResult add(@RequestBody GptWebaccount dto){

        log.debug("--账号库：添加");
        MyResult result = new MyResult();

        try {
            result = gptWebaccountServiceImpl.add(dto);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("账号库-添加-异常");
        }
        return result;
    }

    @PostMapping("/uploadToAdd")
    @ApiOperation("账号库-账号上传添加")
    @MySysLog(modelName="账号库",methodDesc = "账号上传添加",methodType = MySysLogOpration.POST)
    public MyResult uploadPlatform(MultipartFile file) throws IOException {
        log.debug("账号库-账号上传添加"+file);
        MyResult result = new MyResult();

        boolean isErr = false;

        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        List<GptWebaccount> gptWebaccounts = reader.readAll(GptWebaccount.class);
        List<GptWebaccountInfo> infos = new ArrayList<>();
        for(GptWebaccount temp: gptWebaccounts){
            if(temp.getAccountCode() == null){
                isErr = true;
                break;
            }
            temp.setWebAccountID(IdUtil.fastSimpleUUID());
            temp.setRegisterTime(new Date());
            temp.setWebAccountCode("1");
            temp.setWebAccountName("在库");

            // 进行记录
            GptWebaccountInfo info = new GptWebaccountInfo();
            info.setWebAccountID(temp.getWebAccountID());
            info.setInfoTime(new Date());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            info.setInfoDesc("【"+temp.getWebAccountTypeName()+"账号】"+temp.getAccountCode()+"，于"+sdf.format(temp.getRegisterTime())+"注册，然后添加进入账号库");
            infos.add(info);
        }

        if(!isErr){
            gptWebaccountServiceImpl.saveBatch(gptWebaccounts);
            gptWebaccountInfoServiceImpl.saveBatch(infos);
            result.setMsg("上传成功！");
        }else{
            result.setMsg("上传模板错误，请使用标准模板！");
            result.setCode("9999");
        }

        return result;
    }

    @PutMapping("/update")
    @ApiOperation("账号库-修改")
    @MySysLog(modelName="账号库",methodDesc = "修改",methodType = MySysLogOpration.PUT)
    public MyResult update(@RequestBody GptWebaccount dto){

        log.debug("--账号库：修改");
        MyResult result = new MyResult();

        try {
            result = gptWebaccountServiceImpl.update(dto);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("账号库-修改-异常");
        }
        return result;
    }

    @PutMapping("/updateGptLimit")
    @ApiOperation("账号库-更新api调用余额信息")
    @MySysLog(modelName="账号库",methodDesc = "更新api调用余额信息",methodType = MySysLogOpration.PUT)
    public MyResult updateGptLimit(@RequestBody GptWebaccount dto){

        log.debug("--账号库：更新api调用余额信息");
        MyResult result = new MyResult();

        try {
            result = gptWebaccountServiceImpl.updateGptLimit(dto);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("账号库-更新api调用余额信息-异常");
        }
        return result;
    }

    @PutMapping("/updateAllGptLimit")
    @ApiOperation("账号库-更新全部账号库余额信息")
    @MySysLog(modelName="账号库",methodDesc = "更新全部账号库余额信息",methodType = MySysLogOpration.PUT)
    public MyResult updateAllGptLimit(){

        log.debug("--账号库：更新全部账号库余额信息");
        MyResult result = new MyResult();

        try {
            result = gptWebaccountServiceImpl.updateAllGptLimit();
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("账号库-更新全部账号库余额信息-异常");
        }
        return result;
    }

    @PutMapping("/infoChangePwd")
    @ApiOperation("账号库-记录密码修改")
    @MySysLog(modelName="账号库",methodDesc = "记录密码修改",methodType = MySysLogOpration.PUT)
    public MyResult infoChangePwd(@RequestBody GptWebaccount dto){

        log.debug("--账号库：记录密码修改");
        MyResult result = new MyResult();

        try {
            result = gptWebaccountServiceImpl.infoChangePwd(dto);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("账号库-记录密码修改-异常");
        }
        return result;
    }

    @PutMapping("/saleAccount")
    @ApiOperation("账号库-记录账号售出")
    @MySysLog(modelName="账号库",methodDesc = "记录账号售出",methodType = MySysLogOpration.PUT)
    public MyResult saleAccount(@RequestBody GptWebaccount dto){

        log.debug("--账号库：记录账号售出");
        MyResult result = new MyResult();

        try {
            result = gptWebaccountServiceImpl.saleAccount(dto);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("账号库-记录账号售出-异常");
        }
        return result;
    }

    @PutMapping("/lessAccount")
    @ApiOperation("账号库-账号出租")
    @MySysLog(modelName="账号库",methodDesc = "账号出租",methodType = MySysLogOpration.PUT)
    public MyResult lessAccount(@RequestBody GptWebaccount dto){

        log.debug("--账号库：账号出租");
        MyResult result = new MyResult();

        try {
            result = gptWebaccountServiceImpl.lessAccount(dto);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("账号库-账号出租-异常");
        }
        return result;
    }

    @DeleteMapping("/deletes")
    @ApiOperation("账号库-删除")
    @MySysLog(modelName="账号库",methodDesc = "删除",methodType = MySysLogOpration.DELETE)
    public MyResult deletes(String ids){

        log.debug("--账号库：删除");
        MyResult result = new MyResult();

        try {
            result = gptWebaccountServiceImpl.deletes(ids);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("账号库-删除-异常");
        }
        return result;
    }


    
}
